//
//  S1mpl3SpecialHotViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "S1mpl3SpecialHotViewController.h"
#import "TabBarController.h"
#import "TwoToOne.h"
#import "S1mpl3SpecialHot.h"
#import "S1mpl3SpecialCollectionViewCell.h"
#import "FitHeader.h"
#import "S1mpl3CollectionReusableView.h"
#import "MJRefresh.h"
#import "S1mpl3HotListenViewController.h"

@interface S1mpl3SpecialHotViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,strong)NSMutableDictionary *dataSourceDic;
@property(nonatomic,strong)UICollectionView *recommendCollectionView;
@property(nonatomic,strong)NSMutableArray *sectionArray;
@end

@implementation S1mpl3SpecialHotViewController

-(void)viewWillAppear:(BOOL)animated{
    [[TabBarController shareInstance] remainButtonOfPlay:YES];
}



-(void)viewWillDisappear:(BOOL)animated{
    [[TabBarController shareInstance] remainButtonOfPlay:NO];
}

#pragma mark --数据解析

+(NSString *)shareStandardDateByTimeSecond:(NSInteger)timeSecond
{
    //时间戳转标准时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy.MM"];
    //设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSString *date = [NSString stringWithFormat:@"%@", [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timeSecond]]];
    NSArray *array = [date componentsSeparatedByString:@"."];
    NSString *timeDate = [NSString stringWithFormat:@"%@/%@", array[1], array[0]];
    return timeDate;
}

-(void)getData{

    [TwoToOne getDataByString:[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_list?device=android&page=%ld&per_page=10", _page] BodyString:nil WithDataBlock:^(id data) {
        for (NSDictionary *dic in [data objectForKey:@"list"]) {
            if ([[data objectForKey:@"list"] count] == 0) {
                [_recommendCollectionView.mj_footer endRefreshing];
                _recommendCollectionView.mj_footer.state = MJRefreshStateNoMoreData;
                return ;
            }
            
            S1mpl3SpecialHot *s1mpl3SpecialHot = [[S1mpl3SpecialHot alloc] init];
            [s1mpl3SpecialHot setValuesForKeysWithDictionary:dic];
            NSString *keyString = [S1mpl3SpecialHotViewController shareStandardDateByTimeSecond:[s1mpl3SpecialHot.releasedAt integerValue] / 1000];

            if (![[_dataSourceDic allKeys] containsObject:keyString]) {
                NSMutableArray *array = [NSMutableArray array];
                [array addObject:s1mpl3SpecialHot];
                [_dataSourceDic setObject:array forKey:keyString];
                [_sectionArray addObject:keyString];
            }else{
                [[_dataSourceDic objectForKey:keyString] addObject:s1mpl3SpecialHot];
            }
        }


        [_recommendCollectionView reloadData];
        [_recommendCollectionView.mj_header endRefreshing];
        [_recommendCollectionView.mj_footer endRefreshing];
    }];
}

-(void)loadView{
    [super loadView];
    _dataSourceDic = [NSMutableDictionary dictionary];
    _sectionArray = [NSMutableArray array];
    _page = 1;
    [self getData];
    UICollectionViewFlowLayout *contentFlowlayout = [[UICollectionViewFlowLayout alloc] init];
    contentFlowlayout.minimumInteritemSpacing = 0;
    contentFlowlayout.minimumLineSpacing = 0;
    contentFlowlayout.sectionInset = UIEdgeInsetsMake(0, 0, 10 * FITHeight, 0);
    contentFlowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _recommendCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375 * FITWidth, 675 * FITHeight - 64) collectionViewLayout:contentFlowlayout];
    _recommendCollectionView.dataSource = self;
    _recommendCollectionView.delegate = self;
    _recommendCollectionView.showsVerticalScrollIndicator = NO;
    _recommendCollectionView.backgroundColor = [UIColor colorWithRed:0.93 green:0.92 blue:0.91 alpha:1];
    [self.view addSubview:_recommendCollectionView];

    //Secion注册
    [_recommendCollectionView registerClass:[S1mpl3CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    
    //精品听单注册
    [_recommendCollectionView registerClass:[S1mpl3SpecialCollectionViewCell class] forCellWithReuseIdentifier:@"specialIdentifier"];
    
    
    _recommendCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataSourceDic removeAllObjects];
        [_sectionArray removeAllObjects];
        _page = 1;
        [self getData];
    }];
    _recommendCollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getData];
    }];

}

#pragma mark -- CollectionView代理方法
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   
        S1mpl3SpecialCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"specialIdentifier" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
    if (_sectionArray.count != 0) {
        cell.specialHot = [[_dataSourceDic objectForKey:[_sectionArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    }
        return cell;

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(self.view.frame.size.width, 30 * FITHeight);
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    S1mpl3CollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
    view.label.text = _sectionArray[indexPath.section] ;
    return view;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return [_sectionArray count];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [[_dataSourceDic objectForKey:[_sectionArray objectAtIndex:section]] count];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(375 * FITWidth, 120 * FITHeight);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    S1mpl3HotListenViewController *s1mpl3HotListenVC = [[S1mpl3HotListenViewController alloc] init];
    S1mpl3SpecialHot *s1 = [[_dataSourceDic objectForKey:_sectionArray[indexPath.section]] objectAtIndex:indexPath.row];
    //NSLog(@"%@", s1);
//    NSLog(@"%@", [NSString stringWithFormat:@"%@", [[[_dataSourceDic objectForKey:_sectionArray[indexPath.section]] objectAtIndex:indexPath.row] specialID]]);
    
    s1mpl3HotListenVC.specialID = s1.specialId;
    s1mpl3HotListenVC.contentType = s1.contentType;
    [self.navigationController pushViewController:s1mpl3HotListenVC animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"精品听单";
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
